package com.example.flashsales.service.impl;

import com.example.flashsales.entity.dto.StockDetailsDto;
import com.example.flashsales.entity.dto.StockDto;
import com.example.flashsales.entity.pojo.StockRecord;
import com.example.flashsales.entity.pojo.StockRecordDetails;
import com.example.flashsales.entity.vo.StockDetailsVo;
import com.example.flashsales.entity.vo.StockVo;
import com.example.flashsales.mappers.StockMapper;
import com.example.flashsales.service.StockService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class StockServiceImpl implements StockService {


    @Autowired
    StockMapper stockMapper;

    /**
     * 增加一条进货纪录
     * @param stockDto （金额，店家名称）
     * @param stockDetailsDtos（goodsId,count,price）
     * @return
     */
    @Override
    @Transactional
    public boolean addStockRecord(StockDto stockDto, List<StockDetailsDto> stockDetailsDtos) {
        double money = 0.0;
        for (StockDetailsDto stockDetailsDto : stockDetailsDtos) {
            money = (stockDetailsDto.getCount()*stockDetailsDto.getPrice())+money;
        }
        stockDto.setMoney(money);
        if (stockMapper.addStockRecord(stockDto) > 0) {
            //进货订单id
            final long id = stockDto.getId();
            for (StockDetailsDto stockDetailsDto : stockDetailsDtos) {
                stockDetailsDto.setStockId(id);
                System.out.println(stockDetailsDto);
                stockDetailsDto.setMoney(stockDetailsDto.getCount()*stockDetailsDto.getPrice());
                if (!addStockRecordDetails(stockDetailsDto)) {
                    return false;
                }
            }
            return true;
        }
        return false;
    }


    /**
     * 增加一条进货细节
     * @param stockDetailsDto
     * @return
     */
    @Override
    public boolean addStockRecordDetails(StockDetailsDto stockDetailsDto) {
        stockDetailsDto.setMoney(stockDetailsDto.getCount()*stockDetailsDto.getPrice());
        return stockMapper.addStockRecordDetails(stockDetailsDto) > 0;
    }


    /**
     * 查看所有进货纪录
     * @param num
     * @param page
     * @return
     */
    @Override
    public List<StockVo> viewAllStock(int num, int page, Date qDate, Date hDate) {
        if (page <= 0){
            return null;
        }
        final List<StockRecord> stockRecords = stockMapper.viewAllStock(num, (page - 1) * num,qDate,hDate);
        List<StockVo> list = new ArrayList<>();
        for (StockRecord stockRecord : stockRecords) {
            StockVo stockVo = new StockVo();
            BeanUtils.copyProperties(stockRecord,stockVo);
            final List<StockDetailsVo> list1 = stockMapper.viewDetails(stockRecord.getId());
            stockVo.setStockDetailsVos(list1);
            list.add(stockVo);
        }
        return list;
    }

    /**
     * 查看数量
     * @return
     */
    @Override
    public Integer viewCount() {
        return stockMapper.viewCount();
    }
}
